/*
 * Base64StringEncoder.java
 *
 * Created on 5 październik 2007, 20:45
 *
 */

package kosciak.blip4j.utils.base64;

import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.StringEncoder;
import org.apache.commons.codec.binary.Base64;


/**
 * Utility class for encoding String using Bas64
 * @author Wojtek 'KosciaK' Pietrzok
 * @since 0.1
 */
public class Base64StringEncoder extends Base64 implements StringEncoder{
    
    /** Creates a new instance of Base64StringEncoder */
    public Base64StringEncoder() {
    }

    /**
     * Encodes a String using Base64 encoding
     * @param string String to encode
     * @return encoed String
     * @throws org.apache.commons.codec.EncoderException if an error occurs
     */
    public String encode(String string) throws EncoderException {
        
        byte[] bytes, encoded_bytes;
        String encoded_string = "";
        
        try {
            bytes = string.getBytes("UTF-8");
            encoded_bytes = encodeBase64(bytes);
            encoded_string = new String(encoded_bytes);
        
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }
        return encoded_string;
    }
    
}
